package com.september.mongo.entities;

import java.io.UnsupportedEncodingException;
import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;
import java.util.UUID;

import com.google.code.morphia.annotations.Entity;
import com.google.code.morphia.annotations.Id;
import com.september.util.BaseData;

@Entity
public class Salt extends BaseData {
	
	private final String SALT = ""+UUID.randomUUID();
	
	@Id
	private String user;
	private String digest;
	//private Salt() {};
	
	public Salt() {
		
	}
	
	public Salt(String user, String password) {
		
		try {
			this.user = user;
			this.digest = calculateDigest(password);
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}
	
	public  String calculateDigest(String password) throws NoSuchAlgorithmException, UnsupportedEncodingException {
		String saltedPassword = SALT + password;
		MessageDigest md = MessageDigest.getInstance("SHA-256");
		 byte[] bytedPassword = saltedPassword.getBytes("UTF-8");
		 byte[] digest = md.digest(bytedPassword);
		 StringBuffer hexString = new StringBuffer();
	    	for (int i=0;i<digest.length;i++) {
	    	  hexString.append(Integer.toHexString(0xFF & digest[i]));
	    	}
	    return hexString.toString();
	}
	public String getUser() {
		return user;
	}

	public void setUser(String user) {
		this.user = user;
	}

	public String getDigest() {
		return digest;
	}



	@Override
	public String[] getOrdreredField() {
		// TODO Auto-generated method stub
		return null;
	}

	@Override
	public void manageRefernces() {
		// TODO Auto-generated method stub

	}

}
